package org.meeting.radish.controller;

import lombok.extern.slf4j.Slf4j;
import org.meeting.radish.entity.Question;
import org.meeting.radish.entity.TestCase;
import org.meeting.radish.service.QuestionService;
import org.meeting.radish.service.TestCaseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/questions")
public class QuestionController {

    @Resource
    QuestionService questionService;

    @Resource
    TestCaseService testCaseService;

    @GetMapping("/all")
    public String getAllQuestion(Map<String, Object> map,
                                 @CookieValue(value = "uid", required = false) Integer uid,
                                 @CookieValue(value = "status", defaultValue = "0") Integer status) {

        List<Question> questions = questionService.queryOnceQuestion(uid);
        map.put("questions", questions);
        map.put("status", status);
        return "tables";
    }

    @GetMapping("/add")
    public String addQuestion() {
        return "forms";
    }

    @PostMapping("/add")
    public String addQuestion(Question question) {
        questionService.addQuestion(question);
        log.info("{}", question);
        return "redirect:/questions/all";
    }

    @GetMapping("/update/{qid}")
    public String updateQuestion(@PathVariable("qid") Integer qid, Map<String, Object> map) {
        Question question = questionService.queryQuestionByQid(qid);
        List<TestCase> testCases = testCaseService.queryTestCaseByQid(qid);
        map.put("que", question);
        map.put("testCases", testCases);
        return "forms";
    }

    @PostMapping("/update")
    public String updateQuestion(Question question) {
        questionService.updateQuestion(question);
        return "redirect:/questions/all";
    }

    @GetMapping("/delete/{qid}")
    public String deleteQuestion(@PathVariable("qid") Integer qid) {
        questionService.deleteQuestion(qid);
        log.warn("题目{}已被删除", qid);
        return "redirect:/questions/all";
    }

}
